﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Drawing;

namespace ConfExpert.Forms.Espresso
{
    public partial class _Fiyat : XForm
    {
        string Tip;
        public _Fiyat(string Tip)
        {
            InitializeComponent();
            this.Tip = Tip;
        }

        bool EstiCemal;
        private void Fiyat_Load(object sender, EventArgs e)
        {
            
            bbiTip.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            EstiCemal = ytk(121);

            gv.Init("GetEspresso", 2);

            gv.MakeColumnInvisible("idx", "GrupESPId");

            gv.SetCaptions("Width:Width (±%3)", "Weight:Weight (±%3)");

            gv.Columns["Width"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gv.Columns["Width"].DisplayFormat.FormatString = "#,###.## cm'.'";

            gv.Columns["Weight"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gv.Columns["Weight"].DisplayFormat.FormatString = "#,###.## gr'/'m2";

            foreach (string str in "ShirtCalculationPrice;FabricSalesPrice;OzelRenk500_SCP;OzelRenk300_SCP;OzelRenk50_SCP".Split(';'))
            {
                gv.Columns[str].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                gv.Columns[str].DisplayFormat.FormatString = "€ #,###.00";
            }

            gv.PaintAppearance.EvenRow.BackColor = Color.FromArgb(242, 220, 219);
            gv.PaintAppearance.HorzLine.BackColor = Color.FromArgb(218, 150, 150);

            gv.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);

            //gridView1.GroupFormat = "[#image]{1}"; //"{0}: [#image]{1} {2}".

            bbiKaydet.ItemClick += bbiKaydet_ItemClick;
            bbiKaydet.Caption = EstiCemal ? "&Kaydet" : "&Güncelle";

            //gv.Columns["Booklet"].ColumnEdit = RI(Rp.GrupESP);
            gv.OptionsBehavior.Editable = EstiCemal;
            gv.AddSummary();

            //gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);
            //gv.ShowFindPanel();

            //gridView1.CustomColumnDisplayText += new CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText);


            //gv.OptionsView.AllowCellMerge = true;
            //gv.CellMerge += new CellMergeEventHandler(gv_CellMerge);
            //gv.BestFitColumns();

            Rfrsh();
            
            //gv.CustomColumnDisplayText+=new CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText);
        }

        private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "GrupESPId")
                if (e.Value != null)
                    if (intParse(e.Value) > 0)
                        e.DisplayText = (Convert.ToInt32(e.Value) + 1) + ". " + e.DisplayText; // +". " + AppSession.Lookups.GrupESP.FindByGrupESPId(Convert.ToInt32(e.Value) - 1).Name;
        }
        

        private void Rfrsh()
        {
            gv.Columns["Booklet"].Visible = false;
            switch (Tip)
            {
                case "Booklet bazında":
                    this.Text = "2011 FİRMAİÇİ Booklet bazında fiyat listesi";
                    gv.Columns["ShirtCalculationPrice"].VisibleIndex = gv.Columns["FabricSalesPrice"].VisibleIndex;
                    gv.Columns["GrupESPId"].Group();
                    gv.ExpandAllGroups();
                    break;
                case "Alfabetik":
                    this.Text = "2011 FİRMAİÇİ Alfabetik fiyat listesi";
                    gv.Columns["ShirtCalculationPrice"].VisibleIndex = gv.Columns["FabricSalesPrice"].VisibleIndex;
                    gv.Columns["GrupESPId"].VisibleIndex = gv.Columns["KaliteId"].VisibleIndex + 1;
                    //gv.Columns["Booklet"].Visible = false;
                    break;
                //case "2012 FOB Fabric Sales Price List":
                //    this.Text = "2012 FOB Fabric Sales Price List";
                //    gv.Columns["ShirtCalculationPrice"].Visible = false;
                //    gv.Columns["Booklet"].Group();
                //    gv.ExpandAllGroups();
                //    break;
            }

            gv.BestFitColumns();
        }


        public void gv_CellMerge(object sender, CellMergeEventArgs e)
        {
            GridView view = sender as GridView;
            e.Merge = view.GetRowCellDisplayText(e.RowHandle1, e.Column) == view.GetRowCellDisplayText(e.RowHandle2, e.Column) &&
                e.Column.FieldName.Contains("Booklet");
            /*
                view.GetRowCellDisplayText(e.RowHandle1, "ProductId") == view.GetRowCellDisplayText(e.RowHandle2, "ProductId") &&
                !e.Column.FieldName.Contains("Miktar");
            */
            e.Handled = true;
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (EstiCemal)
            {
                gv.EndEditing();
                gv.Update("", "EspMaliyet");
            }

            gv.RefreshDataSource("GetEspresso", 2);
            Rfrsh();
        }
        /*
        private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "Booklet")
                if (e.Value != null)
                    if (intParse(e.Value) > 0)
                        e.DisplayText = e.Value.ToString() + ". " + AppSession.Lookups.GrupESP.FindByGrupESPId(Convert.ToInt32(e.Value) - 1).Name;
        }
        */


        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            switch (e.Column.FieldName)
            {
                case "KaliteId":
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    break;
            }
        }




    }
}
